<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input clearable style="width: 240px" v-model="queryParams.search.keyword" placeholder="请输入商品名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getDataList"></right-toolbar>
    </el-row>
    <el-table :data="dataList" v-loading="loading" style="width: 100%;">

      <el-table-column type="index" label="序号" width="60"></el-table-column>
      <el-table-column prop="name" label="盘点名称" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="stockHouseName" label="仓库" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="inventoryDate" label="盘点日期" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="inventoryUser" label="盘点人" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="操作" fixed="right" align="center" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button size="mini" type="text" @click="preview(scope.row)"
            v-hasPermi="['business:driver:list']">查看</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['business:driver:list']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getDataList" />
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <el-dialog title="盘点信息" :visible.sync="showInfo" width="60%">
      <h3>盘点信息</h3>
      <el-descriptions :column="3">
        <el-descriptions-item label="盘点名称">{{info.name}}</el-descriptions-item>
        <el-descriptions-item label="仓库">{{info.stockHouseName}}</el-descriptions-item>
        <el-descriptions-item label="盘点日期">{{info.inventoryDate}}</el-descriptions-item>
        <el-descriptions-item label="盘点人">{{info.inventoryUser}}</el-descriptions-item>
        <el-descriptions-item label="备注">{{info.remark}}</el-descriptions-item>
      </el-descriptions>
      <h3>货品信息</h3>
      <el-table :data="info.inventoryListDetails" style="margin-top:20px" height="300">
        <el-table-column prop="goodsName" label="商品名称" width="500" align="center" />
        <el-table-column prop="unit" label="单位" align="center" />
        <el-table-column prop="expectQuantity" label="预期数量" align="center" />
        <el-table-column label="点出数量" prop="realQuantity" align="center"></el-table-column>

        <el-table-column prop="profitOrLoss" label="盘盈盘亏" align="center">
          <template slot-scope="scope">
            <div>
              <span class="points ying" v-if="scope.row.realQuantity - scope.row.expectQuantity>0">盘盈</span>
              <span class="points kui" v-else-if="scope.row.realQuantity - scope.row.expectQuantity<0">盘亏</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="盈亏数量" align="center">
          <template slot-scope="scope">
            {{scope.row.realQuantity - scope.row.expectQuantity}}
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showInfo = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  import {
    page,
    deleteById
  } from "@/api/business/stock/inventory";
  import AddOrUpdate from './addOrUpdate'
  import {
    mapState
  } from 'vuex';
  export default {
    computed: {
      ...mapState({
        sysUser: state => state.user.sysUser,
      })
    },
    data() {
      return {
        activeName: '1',
        showInfo: false,
        info: {},
        driverList: [],
        loading: true,
        loading2: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        // 总条数
        total: 0,
        //表格数据
        dataList: [],
        // 弹出层标题
        title: "",
        // 表单参数
        // 显示搜索条件
        showSearch: false,
        form: {},
        // 日期范围
        dateRange: [],
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          search: {
            keyword: '',
          }
        },
        dataList: [],
        rules: {},
        addOrUpdateVisible: false,
        orderList: [],
        comerecordList: [],
        dataDate: []
      }
    },
    components: {
      AddOrUpdate
    },
    created() {
      this.getDataList();
      this.initPage();
    },
    methods: {
      handleDelete(row) {
        const ids = row.id;
        this.$modal.confirm('确定要删除选中的数据项？').then(function() {
          return deleteById(ids);
        }).then(() => {
          this.getDataList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      initPage() {

      },
      preview(e) {
        this.info = e;
        this.showInfo = true;
      },
      // 获取数据列表
      getDataList() {
        this.loading = true;
        this.queryParams.search.stockHouseId = this.sysUser.agentId

        page(this.queryParams).then(response => {
          this.dataList = response.data.list;
          this.total = parseInt(response.data.total);
          this.loading = false;
        }).catch(res => {
          this.loading = false
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getDataList();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.userId);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.addOrUpdateVisible = true;
        this.title = "添加";
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init({})
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.addOrUpdateVisible = true;
        this.title = "修改";
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row)
        })
      },

      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          search: {
            keyword: '',
            franchiseeId: ''
          }
        };
        this.getDataList()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .driver_data {
    position: relative;

    .date {
      position: absolute;
      top: 0;
      right: 0;
      z-index: 1;
    }
  }

  .points {
    display: block;
    display: flex;
    align-items: center;
    justify-content: center;

    &::before {
      display: block;
      content: '';
      width: 6px;
      height: 6px;
      border-radius: 50%;
      margin-right: 5px;
    }
  }

  .ying {
    &::before {
      background: #52c41a;
    }
  }

  .kui {
    &::before {
      background: #f5222d;
    }
  }
</style>
